package com.day10.study.swing_demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TabPanelFrame extends JFrame {
    private static final int WINDOW_WIDTH = 640;
    private static final int WINDOW_HEIGHT = 640;

    private JTabbedPane tabPanel;
    private JPanel p1, p2, p3;
    private JButton btn;

    public TabPanelFrame() {
        initComponent();
        addComponent();
        addListener();
        initWindows();
    }

    private void initComponent() {
        btn = new JButton("选中第3个选项卡");
        tabPanel = new JTabbedPane();
        p1 = new JPanel();
        p1.setBackground(Color.YELLOW);
        p2 = new JPanel();
        p2.setBackground(Color.BLUE);
        p3 = new JPanel();
        p3.setBackground(Color.RED);

    }

    private void addComponent() {
        p1.add(btn);
        tabPanel.add("选项卡A", p1);
        tabPanel.add("选项卡B", p2);
        tabPanel.add("选项卡C", p3);
        this.add(tabPanel);
    }

    private void addListener() {
        btn.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        tabPanel.setSelectedIndex(2);
                    }
                }
        );
    }

    private void initWindows() {
        this.setTitle("TabPanelFrame");
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new TabPanelFrame();
    }
}
